package sjtu.ist.warehouse4ws.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.annotations.JSON;

import sjtu.ist.warehouse4ws.model.Resource;
import sjtu.ist.warehouse4ws.service.PermissionresourcesService;
import sjtu.ist.warehouse4ws.service.ResourceService;
import sjtu.ist.warehouse4ws.service.cust.CustPermissionresourcesService;
import sjtu.ist.warehouse4ws.service.cust.CustResourceService;

import com.opensymphony.xwork2.ActionSupport;
/**
 * by Steve Jin
 */

public class ShowResourceAction extends ActionSupport{
	

	private static final long serialVersionUID = 7797460115963418903L;
	private List<Resource> resourceList=new ArrayList<Resource>();
    private ResourceService resourceService;
    private CustResourceService custResourceService;
    private String resourceid;
    private String permissionid;
    private PermissionresourcesService permissionresourcesService;
    private CustPermissionresourcesService custPermissionresourcesService;
    

	public String getResourceid() {
		return resourceid;
	}
	public void setResourceid(String resourceid) {
		this.resourceid = resourceid;
	}
	public String getPermissionid() {
		return permissionid;
	}
	public void setPermissionid(String permissionid) {
		this.permissionid = permissionid;	}
	
	
	public PermissionresourcesService getPermissionresourcesService() {
		return permissionresourcesService;
	}
	public void setPermissionresourcesService(
			PermissionresourcesService permissionresourcesService) {
		this.permissionresourcesService = permissionresourcesService;
	}
	public ResourceService getResourceService() {
		return resourceService;
	}
	public void setResourceService(ResourceService resourceService) {
		this.resourceService = resourceService;
	}
	
	public String showResource(){
		
		HttpServletRequest request = ServletActionContext.getRequest ();
		this.permissionid=request.getParameter("cid");
		
		Subject currentUser = SecurityUtils.getSubject();
		boolean admin=currentUser.hasRole("superadmin");
		if(admin){
			List <String> resourceids=permissionresourcesService.getResourcesByPermissionid(permissionid);			
			
			for(String a : resourceids){
				System.out.println(resourceService.getResourceById(a));
				System.out.println("resource!!!!!!!!!");
				this.resourceList.addAll(resourceService.getResourceById(a));
			}

			System.out.println(resourceList.size());
		}else{
			List <String> resourceids=custPermissionresourcesService.getResourcesByPermissionid(permissionid);			
			
			for(String a : resourceids){
				System.out.println(resourceService.getResourceById(a));
				System.out.println("resource!!!!!!!!!");
				this.resourceList.addAll(resourceService.getResourceById(a));
			}

			System.out.println(resourceList.size());
		}
		
	    
	  
	  	return SUCCESS;										//获取得到的数据
		
	}
	@JSON(name = "totalResource")	
	public int getTotalAttribute() {
		return this.resourceList.size();
	}
    
	
	@JSON(name ="resource")	
	public List<Resource> getResourceList() {
		return this.resourceList;
	}
	/**
	 * @param custResourceService the custResourceService to set
	 */
	public void setCustResourceService(CustResourceService custResourceService) {
		this.custResourceService = custResourceService;
	}
	/**
	 * @return the custResourceService
	 */
	public CustResourceService getCustResourceService() {
		return custResourceService;
	}
	/**
	 * @param custPermissionresourcesService the custPermissionresourcesService to set
	 */
	public void setCustPermissionresourcesService(
			CustPermissionresourcesService custPermissionresourcesService) {
		this.custPermissionresourcesService = custPermissionresourcesService;
	}
	/**
	 * @return the custPermissionresourcesService
	 */
	public CustPermissionresourcesService getCustPermissionresourcesService() {
		return custPermissionresourcesService;
	}
    
}
